本章使用simple_peripheral作为实验平台。创建一个自定义属性的characteristic。simple_peripheral的IAR工程可以在目录C:\ti\simplelink_cc2640r2_sdk_1_35_00_33\examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\tirtos\iar下找到。
有关工程配置和编译选项以及下载方法参考CC2640R2 BLE 开发环境搭建部分和使用IAR进行开发。
使用UBS连接我们的开发板。如下图所示
成功连接开发板之后,打开电脑的设备管理器。可以看见XDS110的两个端口,BLE Device Monitor将会使用XDS110 Class Application/User UART(COM25)
注意:
XDS110驱动程序在安装IAR时会默认安装,如果你没有安装IAR,可以手动更新驱动程序软件,XDS驱动可以在CC2640R2开发工具集介绍里获得。
协议栈和IAR安装参考CC2640R2 BLE 开发环境搭建,本例程需要使用BLE Device Monitor,有关BLE Device Monitor使用介绍请参考:BLE Device Monitor 所有工具可以在CC2640R2开发工具集介绍里获得。
有关BLE Device Monitor环境搭建以及使用方法请参考:BLE Device Monitor。
在simple_perpheral工程中添加一个characteristic 6,主要通过修改simple_gatt_profile.c和simple_perpheral.c两个文件完成。
//source\ti\ble5stack\profiles\simple_profile\simple_gatt_profile.h SIMPLEPROFILE_CHAR6 line 87
#define SIMPLEPROFILE_CHAR6 5 //特征值
#define SIMPLEPROFILE_CHAR6_UUID 0xFFF6 //特征值UUID
#define SIMPLEPROFILE_CHAR6_LEN 5 //特征值长度
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c charValue6 line 618
uint8_t charValue6[SIMPLEPROFILE_CHAR6_LEN] = { 'a', 'b', 'c', 'd', 'e' };
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleProfile_SetParameter line 629
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR6, SIMPLEPROFILE_CHAR6_LEN,
charValue6);
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleProfile_SetParameter line 547
case SIMPLEPROFILE_CHAR6:
if ( len == SIMPLEPROFILE_CHAR6_LEN ) {
VOID memcpy( simpleProfileChar6, value, SIMPLEPROFILE_CHAR6_LEN );
// See if Notification has been enabled
GATTServApp_ProcessCharCfg( simpleProfileChar6Config, simpleProfileChar6, FALSE,
simpleProfileAttrTbl, GATT_NUM_ATTRS( simpleProfileAttrTbl ),
INVALID_TASK_ID, simpleProfile_ReadAttrCB );
} else{
ret = bleInvalidRange;
}break;
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleProfile_GetParameter line 604
case SIMPLEPROFILE_CHAR6:
VOID memcpy( value, simpleProfileChar6, SIMPLEPROFILE_CHAR6_LEN );
break;
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c simpleProfileChar6Props line 197
static uint8 simpleProfileChar6Props = GATT_PROP_NOTIFY;
static gattCharCfg_t *simpleProfileChar6Config;
static uint8 simpleProfileChar6[SIMPLEPROFILE_CHAR6_LEN] = { 0, 0, 0, 0, 0 };
static uint8 simpleProfileChar6UserDesp[17] = "Characteristic 6";
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c simpleProfilechar6UUID line 197
// Characteristic 6 UUID: 0xFFF6
CONST uint8 simpleProfilechar6UUID[ATT_BT_UUID_SIZE] ={
LO_UINT16(SIMPLEPROFILE_CHAR6_UUID), HI_UINT16(SIMPLEPROFILE_CHAR6_UUID)
};
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c simpleProfileAttrTbl line 340
// Characteristic 6 Declaration
{ { ATT_BT_UUID_SIZE, characterUUID },
GATT_PERMIT_READ,
0,
&simpleProfileChar6Props },
// Characteristic Value 6
{ { ATT_BT_UUID_SIZE, simpleProfilechar6UUID },
0,
0,
simpleProfileChar6 },
{ { ATT_BT_UUID_SIZE, clientCharCfgUUID },
GATT_PERMIT_READ | GATT_PERMIT_WRITE,
0,
(uint8 *)&simpleProfileChar6Config },
// Characteristic 6 User Description
{ { ATT_BT_UUID_SIZE, charUserDescUUID },
GATT_PERMIT_READ,
0,
simpleProfileChar6UserDesp },
注意修改simpleProfileAttrTbl[SERVAPP_NUM_ATTR_SUPPORTED]数组大小。SERVAPP_NUM_ATTR_SUPPORTED定义的是17,由于我们添加了4个属性,所以这里需要修改成21。
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleProfile_AddService line 419
simpleProfileChar6Config = (gattCharCfg_t *)ICall_malloc( sizeof(gattCharCfg_t)*linkDBNumConns );
if ( simpleProfileChar6Config == NULL ){
return ( bleMemAllocError );
}
GATTServApp_InitCharCfg( INVALID_CONNHANDLE, simpleProfileChar6Config );
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c simpleProfile_ReadAttrCB line 670
case SIMPLEPROFILE_CHAR6_UUID:
*pLen = SIMPLEPROFILE_CHAR6_LEN;
VOID memcpy( pValue, pAttr->pValue, SIMPLEPROFILE_CHAR6_LEN );
break;
经过上面7步就完成了在增加通知属性的characteristic特征值,我们可以通过按键方式或者将就程序现成的定时器来每秒进行一次通知。这里我们演示使用定时器每秒通知一次characteristic6的值。在simple_peripheral.c文件里SimpleBLEPeripheral_performPeriodicTask函数中增加下面代码。
//examples\rtos\CC2640R2_LAUNCHXL\ble5stack\simple_peripheral\src\app\simple_peripheral.c SimpleBLEPeripheral_performPeriodicTask line 1380
uint8_t charValue6[SIMPLEPROFILE_CHAR6_LEN] = { 'a', 'b', 'c', 'd', 'e' };
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR6, SIMPLEPROFILE_CHAR6_LEN, charValue6);
如下图所示,使用BLE Device Monitor就能接收到开发板发送出来的数据,这里显示的abcde和我们设置的一样。
文章所有代码、工具、文档开源。加入我们QQ群 591679055获取更多支持,共同研究CC2640R2F&BLE5.0。